package com.wxd.schedule;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author wxd
 * @version V1.0
 * @description CustomSchedule
 * @date 2022/10/12 15:24
 **/
@Slf4j
@Component
public class CustomSchedule {

    private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");

    /**
     * 每隔5秒执行一次
     */
    @Scheduled(cron = "0/5 * * * * ? ")
    public void schedule() {
        log.info("schedule -> {}", LocalDateTime.now().format(formatter));
    }

    /**
     * 上一次开始执行时间点之后3000毫秒再执行
     */
    @Scheduled(fixedRate = 3000)
    public void schedule01() {
        log.info("schedule01 -> {}", LocalDateTime.now().format(formatter));
    }

    /**
     * 上一次执行完毕时间点之后3s再执行
     */
    @Scheduled(fixedDelay = 3000)
    public void schedule02() {
        log.info("schedule02 -> {}", LocalDateTime.now().format(formatter));
    }

    /**
     * 第一次延迟2s之后执行, 之后按照每3s执行一次
     */
    @Scheduled(initialDelay = 2000, fixedRate = 3000)
    public void schedule03() {
        log.info("schedule03 -> {}", LocalDateTime.now().format(formatter));
    }

}
